﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace StockMonitor.Business
{
    /// <summary>
    /// 新浪当前价格获取类
    /// </summary>
    /// <remarks>
    /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
    /// 编写时间：<datetime>2012-10-16 4:53 PM</datetime>
    /// </remarks>
    public class SinaCurrentPriceProvider : ICurrentPriceProvider
    {
        /// <summary>
        /// 获得当前股票的价格
        /// </summary>
        /// <param name="stockCode">股票编号</param>
        /// <returns>
        /// 当前价格
        /// </returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:53 PM</datetime>
        /// </remarks>
        public double GetCurrentPrice(string stockCode)
        {
            string result = GetJSbyWC(int.Parse(stockCode) > 599999 ? "sh" + stockCode : "sz" + stockCode);

            return double.Parse(result.Split(',')[3]);
        }

        /// <summary>
        /// 使用WebClient获取行情json
        /// </summary>
        /// <param name="code">股票代码.</param>
        /// <returns></returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:53 PM</datetime>
        /// </remarks>
        private string GetJSbyWC(string code)
        {
            ServicePointManager.DefaultConnectionLimit = 10;

            using (WebClient wc = new WebClient())
            {
                Stream resStream = wc.OpenRead("http://hq.sinajs.cn/list=" + code);
                StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
                string result = sr.ReadToEnd();
                resStream.Close();
                return result;
            }
        }

        /// <summary>
        /// 获取当前买卖价格
        /// </summary>
        /// <param name="stockCode">股票代码</param>
        /// <param name="priceType">价格类型</param>
        /// <returns>
        /// 所需价格
        /// </returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:53 PM</datetime>
        /// </remarks>
        public double GetPrice(string stockCode, Enums.PriceType priceType)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 批量获取股票的当前价格
        /// </summary>
        /// <param name="stockCodes">股票代码列表</param>
        /// <returns>
        /// 结果集
        /// </returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-16 4:53 PM</datetime>
        /// </remarks>
        public List<StockPrice> BatchGetCurrentPrice(List<string> stockCodes)
        {
            throw new NotImplementedException();
        }
    }
}
